package com.reader.cms.controller.magazine;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.reader.cms.controller.BaseController;
import com.reader.cms.entity.magazine.ReaderMagazine;
import com.reader.cms.mapper.magazine.ReaderMagazineMapper;
import com.reader.cms.service.magazine.IReaderMagazineService;
import com.reader.core.entity.ResultMessageInfo;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

import com.reader.core.utils.ValidatorUtil;
import com.reader.core.validator.group.AddGroup;
import com.baomidou.mybatisplus.plugins.Page;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author gumenghao
 * @description 杂志有关接口
 * @date 2018-10-23
 */
@RestController
@RequestMapping("/api/readermagazine")
public class ReaderMagazineController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ReaderMagazineController.class);

    @Autowired
    public IReaderMagazineService iReaderMagazineService;

    @Autowired
    public ReaderMagazineMapper readerMagazineMapper;

    /**
     * @param currentPage 当前页
     * @param size        每页记录数
     * @param state       状态
     * @return
     * @function 分页查询数据
     */
    @GetMapping("list")
    public ResultMessageInfo selectListPage(@RequestParam(value = "currentPage") int currentPage,
                                            @RequestParam(value = "size") int size,
                                            @RequestParam(value = "state", required = false) String state,
                                            @RequestParam(value = "year", required = false) Integer year,
                                            @RequestParam(value = "period", required = false) String period) {
        ResultMessageInfo resultMessage = null;
        Page<ReaderMagazine> list = iReaderMagazineService.selectListPage(currentPage, size, state, year, period);
        return ResultMessageInfo.ofSuccess("查询成功！", list);
    }

    /**
     * @param readerMagazine 实体对象
     * @return
     * @function 添加新杂志
     */
    @PostMapping("insert")
    public ResultMessageInfo insert(@RequestBody ReaderMagazine readerMagazine) {
        String id = UUID.randomUUID().toString().replace("-", "");
        readerMagazine.setId(id);
        if (readerMagazine.getState().equals("Publish")) {
            readerMagazine.setPublishDt(new Date());
        }
        ValidatorUtil.validateEntity(readerMagazine, AddGroup.class);//非空验证
        EntityWrapper<ReaderMagazine> wrapper = new EntityWrapper<>();
        wrapper.where("year = {0}",readerMagazine.getYear()).where("period = {0}",readerMagazine.getPeriod());
        List<ReaderMagazine> readerMagazines = readerMagazineMapper.selectList(wrapper);
        if (readerMagazines.size() != 0){
            return ResultMessageInfo.ofMessage("刊期已存在！！！");
        }
        ResultMessageInfo resultMessage = null;
        Integer userId = this.getUserId();
        readerMagazine.setInsertDt(new Date());
        readerMagazine.setDelFlag(0);
        readerMagazine.setEnable(1);
        readerMagazine.setInsertUserId(userId);
        readerMagazine.setUpdtUserId(userId);
        boolean resultValue = iReaderMagazineService.insert(readerMagazine);
        if (!resultValue) {
            ResultMessageInfo.ofMessage("添加失败！");
        }
        return ResultMessageInfo.ofSuccess("添加成功！");
    }


    /**
     * @return
     * @function 获取杂志编辑回显数据
     */
    @GetMapping("update_view/{id}")
    public ResultMessageInfo getEditMagazine(@PathVariable(value = "id") String id) {
        ResultMessageInfo resultMessage = null;
        ReaderMagazine readerMagazine = new ReaderMagazine();
        readerMagazine.setId(id);
        readerMagazine.setEnable(1);
        readerMagazine.setDelFlag(0);
        ReaderMagazine magazine = readerMagazineMapper.selectOne(readerMagazine);
        return ResultMessageInfo.ofSuccess("查询成功！", magazine);
    }

    /**
     * @param readerMagazine 实体对象
     * @return
     * @function 更新杂志信息
     */
    @PostMapping("update")
    @Transactional
    @ApiOperation(value = "跟据ID列新记录", notes = "id为必须项")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "要更新对象ID", required = true, dataType = "int", paramType = "path")})
    public ResultMessageInfo update(@RequestBody ReaderMagazine readerMagazine) {
        ValidatorUtil.validateEntity(readerMagazine, AddGroup.class);//非空验证
        ResultMessageInfo resultMessage = null;
        Integer userId = this.getUserId();
        readerMagazine.setUpdtDt(new Date());
        readerMagazine.setUpdtUserId(userId);
        boolean resultValue = iReaderMagazineService.updateById(readerMagazine);
        if (!resultValue) {
            return ResultMessageInfo.ofMessage("修改失败！");

        }
        return ResultMessageInfo.ofSuccess("修改成功！");
    }

    /**
     * @return
     * @function 删除单期杂志
     */
    @PostMapping("delete")
    @Transactional
    public ResultMessageInfo deleteById(@RequestBody ReaderMagazine readerMagazine) {
        Integer userId = this.getUserId();
        readerMagazine.setDelFlag(1);
        readerMagazine.setUpdtDt(new Date());
        readerMagazine.setUpdtUserId(userId);
        boolean resultValue = iReaderMagazineService.updateById(readerMagazine);
        if (!resultValue) {
            ResultMessageInfo.ofMessage("删除失败！");
        }
        return ResultMessageInfo.ofSuccess("删除成功！");

    }

    /**
     * @param data 键为ids值为整型数组的json格式
     * @return
     * @function 批量删除杂志
     */
    @PostMapping("deleteIds")
    @Transactional
    public ResultMessageInfo deleteBatchIds(@RequestBody List<ReaderMagazine> readerMagazines) {
        Integer userId = this.getUserId();
        readerMagazines.forEach(readerMagazine -> {
            readerMagazine.setDelFlag(1);
            readerMagazine.setUpdtDt(new Date());
            readerMagazine.setUpdtUserId(userId);
        });
        boolean resultValue = iReaderMagazineService.updateBatchById(readerMagazines);
        if (!resultValue) {
            ResultMessageInfo.ofMessage("删除失败！");
        }
        return ResultMessageInfo.ofSuccess("删除成功！");
    }
}